<?php

namespace App\Http\Controllers\Admin\Promotion;

use App\Http\Models\Admin\Sys\AdPosition;
use App\Http\Models\Admin\Sys\Advertisement;
use App\Http\Responses\ResponseDto;
use App\Services\Admin\AdService;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Psy\Util\Json;

class AdPositionController extends Controller
{

    public function __construct(AdService $service){
        $this->service = $service;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response{cate}/{subcategory}
     */
    public function index(Request $request)
    {
        return View::make('admin.promotion.ad_position')
            ->withAssets($request['ASSETS']);
    }
    public function page(Request $request)
    {
        $datas = $request->input();
        $start = $datas[3]['value'];
        $length = $datas[4]['value'];
        $res = DB::table('ad_position')->skip($start)->take($length)->get();
        $total = DB::table('ad_position')->count();
//        $list = Config::get("common.ads_location_list");
//        if($list){
//            foreach($res as $item){
//                if($item->key){
//                    $item->location = $list[$item->key];
//                }
//            }
//        }


        return ResponseDto::createResObj(ResponseDto::convertPageToDataTableObj($res,$total),ResponseDto::$SUCCESS);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }



    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {


            $ad = new AdPosition();
            $ad->name = $request->input("name");
            $ad->position = $request->input("position");
            $ad->width = $request->input("width");
            $ad->height = $request->input("height");
            $ad->desc = $request->input("desc");

            if($request->input("id")){
                //更新操作
                $exit_item = AdPosition::find($request->input("id"))->first();
                if($exit_item == null){
                    return ResponseDto::createResObj(trans("admin/message.data_no_exit"),ResponseDto::$FAIL);
                }
                else{
                    $exit_item->name = $ad->name;
                    $exit_item->position = $ad->position;
                    $exit_item->height = $ad->height;
                    $exit_item->width = $ad->width;
                    $exit_item->desc = $ad->desc;
                    $exit_item->save();
                    return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
                }
            }
            else{
                $ad->save();
                return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
            }



    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        return Json::encode(AdPosition::find($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        AdPosition::destroy($id);
        return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
    }
}
